﻿package Server.Poker.NiuNiu10;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;


class tagAnalyseResult {
	int cbFourCount; // 四张数目
	int cbThreeCount; // 三张数目
	int cbLONGCount; // 两张数目
	int cbSignedCount; // 单张数目
	int cbFourLogicVolue[] = new int[1]; // 四张列表
	int cbThreeLogicVolue[] = new int[1]; // 三张列表
	int cbLONGLogicVolue[] = new int[2]; // 两张列表
	int cbSignedLogicVolue[] = new int[5]; // 单张列表
	int cbFourCardData[] = new int[5]; // 四张列表
	int cbThreeCardData[] = new int[5]; // 三张列表
	int cbLONGCardData[] = new int[5]; // 两张列表
	int cbSignedCardData[] = new int[5]; // 单张数目

	public void clear() {
		cbFourCount = 0;
		cbThreeCount = 0;
		cbLONGCount = 0;
		cbSignedCount = 0;
		cbFourLogicVolue[0] = 0;
		cbThreeLogicVolue[0] = 0;
		cbLONGLogicVolue[0] = 0;
		cbLONGLogicVolue[1] = 0;
		for (int i = 0; i < 5; i++) {
			cbSignedLogicVolue[i] = 0;
		}
	}
};

public class GameLogic {

	public	int[]	m_byPokerPai;

	public GameLogic()
	{
		m_byPokerPai = new int[52];
	}
	int random(int iMin, int iMax){
		Random ra =new Random();
		return ra.nextInt(iMax-iMin) + iMin;
	}	


		//扑克数据
		final int m_cbCardListData[] =
		{
			0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,	//方块 A - K
			0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,	//梅花 A - K
			0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D,	//红桃 A - K
			0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D,	//黑桃 A - K
		};

		public int CountArray(int by[] ){
			return by.length;
		}
		public int sizeof(int by[] ){
			return by.length;
		}
		public void CopyMemory(int[] by,int[] by2,int length){
			for(int i = 0; i< length;i++){
				by[i] = by2[i];
			}
		}
		public Random rd = new Random();
		public int rand(){
			return rd.nextInt(1000);
		}
		// 混乱扑克
		void RandCardList(int cbCardBuffer[], int cbBufferCount) {
			//混乱准备	
			System.arraycopy(m_cbCardListData, 0, m_byPokerPai, 0, 52);
		    for(int i = 0;i < 100;i++){
		    	int c1 = random(i*52,10000+i*52) % 52;
		    	int c2 = random(i*52,10000+i*52) % 52;
		    	int tmp = m_byPokerPai[c1];
		    	m_byPokerPai[c1] = m_byPokerPai[c2];
		    	m_byPokerPai[c2] = tmp;
		    }
			for(int i =0 ;i<cbBufferCount;i++)
			{
				cbCardBuffer[i] = m_byPokerPai[i];
			}
			return;
		}
		
		int GetDaPai(int cbCardData[])
		{
			int tmpCardData[] = new int[5];
			System.arraycopy(cbCardData, 0, tmpCardData, 0, 5);
			for(int i =0 ;i<100;i++)
			{
				int rand_Position = random(i*52,10000+i*52) % 52;
				for(int j=0;j<52;j++)
				{
					rand_Position = (rand_Position + j) % 52;
					if(m_byPokerPai[rand_Position] > 0)
					{
						int byPai = m_byPokerPai[rand_Position];
						tmpCardData[4] = byPai;
						int card_type = GetCardType(tmpCardData,5);
						if(card_type == CT_SPECIAL_NIU8 || card_type == CT_SPECIAL_NIU9 || card_type == CT_SPECIAL_NIUNIU)
						{
							m_byPokerPai[rand_Position] = 0;
							return byPai;
						}
					}
				}
			}
			return 0;
		}


		void RandCardListEx(int cbCardBuffer[], int cbBufferCount)
		{
			int cbCardData[] = new int[cbBufferCount];
			CopyMemory(cbCardData, cbCardBuffer, cbBufferCount);

			//混乱扑克
			int cbRandCount = 0;
			int cbPosition = 0;
			do
			{
				cbPosition = rand() % (cbBufferCount - cbRandCount);
				cbCardBuffer[cbRandCount++] = cbCardData[cbPosition];
				cbCardData[cbPosition] = cbCardData[cbBufferCount - cbRandCount];
			} while (cbRandCount < cbBufferCount);
		}
		
		final int  CT_SPECIAL_NIU1 = 3;
		final int  CT_SPECIAL_NIU2 = 4;
		final int  CT_SPECIAL_NIU3 = 5;
		final int  CT_SPECIAL_NIU4 = 6;
		final int  CT_SPECIAL_NIU5 = 7;
		final int  CT_SPECIAL_NIU6 = 8;
		final int  CT_SPECIAL_NIU7 = 9;
		final int  CT_SPECIAL_NIU8 = 10;
		final int  CT_SPECIAL_NIU9 = 11;
		

		final int  CT_SPECIAL_NIUNIU = 12;
		final int  CT_SPECIAL_NIUNIUXW = 13;
		final int  CT_SPECIAL_NIUNIUDW = 14;
		
		final int  CT_SPECIAL_NIUYING = 15;
		
		final int  CT_SPECIAL_NIUKING = 18;
		
		final int  CT_SPECIAL_SHUNZI = 17;
		final int  CT_SPECIAL_TONGHUA = 18;
		final int  CT_SPECIAL_HULU = 19;
		final int  CT_SPECIAL_WUXIAONIU = 20;
		final int  CT_SPECIAL_WUHUANIU = 21;
		final int  CT_SPECIAL_BOMEBOME = 22;
		final int  CT_SPECIAL_TONGHUASHU = 23;
		final int  CT_SPECIAL_CT_KING_TONG_HUA_SHUN = 24;
		
		final int  CT_POINT = 1;
		
		int RetType(int itype)
		{
			itype = itype % 10;
			switch (itype)
			{
			case 0:
				return CT_SPECIAL_NIUNIU;
			case 1:
				return CT_SPECIAL_NIU1;
			case 2:
				return CT_SPECIAL_NIU2;
			case 3:
				return CT_SPECIAL_NIU3;
			case 4:
				return CT_SPECIAL_NIU4;
			case 5:
				return CT_SPECIAL_NIU5;
			case 6:
				return CT_SPECIAL_NIU6;
			case 7:
				return CT_SPECIAL_NIU7;
			case 8:
				return CT_SPECIAL_NIU8;
			case 9:
				return CT_SPECIAL_NIU9;
			default:
				return CT_POINT;
			}

		}
		public int CT_ERROR_CARD = 0;
		public int NIUNIU_CARD_COUNT= 52;
		public	int ST_VALUE		=			1		;							//数值排序
		public	int ST_NEW			=		    3	;								//数值排序
		public	int ST_LOGIC			=		2		;							//逻辑排序
		//获取牌型
		int GetCardType( int cbCardData[], int cbCardCount){
			return GetCardType(cbCardData,cbCardCount,null);
		}
		int GetCardType( int cbCardData[], int cbCardCount, int[] bcOutCadData)
		{
			//合法判断
			if (5 != cbCardCount) return CT_ERROR_CARD;

			//排序扑克
			int cbCardDataSort[] = new int[NIUNIU_CARD_COUNT];
			CopyMemory(cbCardDataSort, cbCardData, cbCardCount);
			SortCardList(cbCardDataSort, cbCardCount, ST_NEW);

			if (bcOutCadData != null)
			{
				CopyMemory(bcOutCadData, cbCardDataSort, cbCardCount);
			}
			
			//////////////////////////////////////////////////////////////////
			int cntZero = 0;
			for(int i=0;i<cbCardCount;i++)
			{
				if(cbCardData[i] == 0)
				{
					cntZero++;
				}
			}
			// 变量定义
			boolean cbSameColor = true, bLineCard = true;
			int cbFirstColor = GetCardColor(cbCardDataSort[0]);
			int cbFirstValue = GetCardValue(cbCardDataSort[0]);

			// 牌形分析
			for (int i = 1; i < cbCardCount; i++) {
				// 数据分析
				if (GetCardColor(cbCardDataSort[i]) != cbFirstColor)
					cbSameColor = false;
				if (cbFirstValue  != (GetCardValue(cbCardDataSort[i]) +i ))
					bLineCard = false;

				// 结束判断
				if ((cbSameColor == false) && (bLineCard == false))
					break;
			}

			// 最大同花顺
			if ((bLineCard == false) && (cbFirstValue == 13)) {
				if(GetCardValue(cbCardDataSort[1]) == 12 && GetCardValue(cbCardDataSort[2]) == 11 && GetCardValue(cbCardDataSort[3]) == 10 && GetCardValue(cbCardDataSort[4]) == 1   )
					bLineCard = false;
			}

			// 皇家同花顺
			if ((cbSameColor == true) && (bLineCard == true) && (GetCardLogicValue(cbCardDataSort[4]) == 1))
				return CT_SPECIAL_CT_KING_TONG_HUA_SHUN;

			// 同花顺类型
			if ((cbSameColor == true) && (bLineCard == true))
				return CT_SPECIAL_TONGHUASHU;
			
		

			// 扑克分析
			tagAnalyseResult AnalyseResult = new tagAnalyseResult();
			AnalysebCardData(cbCardDataSort, cbCardCount, AnalyseResult);

			// 类型判断
			
			// 炸弹
			if (GetCardNewValue(cbCardDataSort[0]) == GetCardNewValue(cbCardDataSort[cbCardCount - 2]))
			{
				if (bcOutCadData != null)
				{
					CopyMemory(bcOutCadData, cbCardDataSort, cbCardCount);
				}
				return CT_SPECIAL_BOMEBOME;
			}
			else
			{
				if (GetCardNewValue(cbCardDataSort[1]) == GetCardNewValue(cbCardDataSort[cbCardCount - 1]))
				{
					if (bcOutCadData != null)
					{
						CopyMemory(bcOutCadData, cbCardDataSort, cbCardCount);
					}
					return CT_SPECIAL_BOMEBOME;
				}

			}
			// 炸弹
			if (GetCardColor(cbCardDataSort[0]) == 0x04 && GetCardColor(cbCardDataSort[1]) == 0x04)
			{
				if (GetCardNewValue(cbCardDataSort[2]) == GetCardNewValue(cbCardDataSort[3]))
				{
					if (bcOutCadData != null)
					{
						bcOutCadData[0] = cbCardDataSort[2];
						bcOutCadData[1] = cbCardDataSort[3];
						bcOutCadData[2] = cbCardDataSort[0];
						bcOutCadData[3] = cbCardDataSort[1];
						bcOutCadData[4] = cbCardDataSort[4];

					}
					return CT_SPECIAL_BOMEBOME;
				}
				else
				{
					if (GetCardNewValue(cbCardDataSort[3]) == GetCardNewValue(cbCardDataSort[4]))
					{
						if (bcOutCadData != null)
						{
							bcOutCadData[0] = cbCardDataSort[0];
							bcOutCadData[1] = cbCardDataSort[1];
							bcOutCadData[2] = cbCardDataSort[3];
							bcOutCadData[3] = cbCardDataSort[4];
							bcOutCadData[4] = cbCardDataSort[2];

						}
						return CT_SPECIAL_BOMEBOME;
					}

				}

			}
			// 炸弹
			if (GetCardColor(cbCardDataSort[0]) == 0x04)
			{
				if (GetCardNewValue(cbCardDataSort[1]) == GetCardNewValue(cbCardDataSort[3]))
				{
					if (bcOutCadData != null)
					{
						bcOutCadData[0] = cbCardDataSort[1];
						bcOutCadData[1] = cbCardDataSort[2];
						bcOutCadData[2] = cbCardDataSort[3];
						bcOutCadData[3] = cbCardDataSort[0];
						bcOutCadData[4] = cbCardDataSort[4];

					}
					return CT_SPECIAL_BOMEBOME;
				}
				else
				{
					if (GetCardNewValue(cbCardDataSort[2]) == GetCardNewValue(cbCardDataSort[4]))
					{
						if (bcOutCadData != null)
						{
							bcOutCadData[0] = cbCardDataSort[2];
							bcOutCadData[1] = cbCardDataSort[3];
							bcOutCadData[2] = cbCardDataSort[4];
							bcOutCadData[3] = cbCardDataSort[0];
							bcOutCadData[4] = cbCardDataSort[1];

						}
						return CT_SPECIAL_BOMEBOME;
					}

				}

			}

			// 五花牛
			boolean blBig = true;
			int iValueTen = 0;
			for (int i = 0; i < cbCardCount; i++)
			{
				int bcGetValue = GetCardLogicValue(cbCardDataSort[i]);
				if (bcGetValue != 10 && bcGetValue != 11)
				{

					blBig = false;
					break;

				}
				else
				{
					if (GetCardNewValue(cbCardDataSort[i]) == 10)
					{
						iValueTen++;
					}
				}
			}
			if (blBig)
			{
				if (bcOutCadData != null)
				{
					CopyMemory(bcOutCadData, cbCardDataSort, cbCardCount);
				}
				if (iValueTen == 0)
					return CT_SPECIAL_WUHUANIU;
				else
				{
					if (iValueTen == 1)
					{
						return CT_SPECIAL_NIUYING;
					}
				}
			}

			//五小牛
			if(GetCardLogicValue(cbCardDataSort[0])<5 && GetCardLogicValue(cbCardDataSort[1])<5 && 
					GetCardLogicValue(cbCardDataSort[2])<5 && GetCardLogicValue(cbCardDataSort[3])<5
					&& GetCardLogicValue(cbCardDataSort[4])<5){
				int niuNumber = GetCardLogicValue(cbCardDataSort[0])+GetCardLogicValue(cbCardDataSort[1])+
						GetCardLogicValue(cbCardDataSort[2])+GetCardLogicValue(cbCardDataSort[3])+GetCardLogicValue(cbCardDataSort[4]);
				if(niuNumber<=10){
					return CT_SPECIAL_WUXIAONIU;
				}
			}

			// 葫芦牛
			if ((AnalyseResult.cbLONGCount == 1) && (AnalyseResult.cbThreeCount == 1))
				return CT_SPECIAL_HULU;
			


			// 同花类型
			if ((cbSameColor == true) && (bLineCard == false))
			{
				if(cbCardDataSort[3] == 0)
				{
					//return Constants.CT_SINGLE;
				}else{
					return CT_SPECIAL_TONGHUA;
				}
			}
			
			// 顺子类型
			if ((cbSameColor == false) && (bLineCard == true))
			{
				if(cbCardDataSort[3] == 0)
				{
					//return Constants.CT_SINGLE;
				}else{
					return CT_SPECIAL_SHUNZI;
				}
			}

			// 银牛
			blBig = true;
			iValueTen = 0;
			for (int i = 0; i < cbCardCount; i++)
			{
				int bcGetValue = GetCardLogicValue(cbCardDataSort[i]);
				if (bcGetValue != 10 && bcGetValue != 11)
				{

					blBig = false;
					break;

				}
				else
				{
					if (GetCardNewValue(cbCardDataSort[i]) == 10)
					{
						iValueTen++;
					}
				}
			}
			if (blBig)
			{
				if (bcOutCadData != null)
				{
					CopyMemory(bcOutCadData, cbCardDataSort, cbCardCount);
				}
				
				if (iValueTen == 1)
				{
					return CT_SPECIAL_NIUYING;
				}
			}


			/////////////////////////////////////////////////////////////////////////////////////
			
			int n = 0;

			int bcMakeMax[] = new int[5];
			memset(bcMakeMax, 0, 5);
			int iBigValue = 0;
			int iSingleA[] = new int[2];
			bcMakeMax[0] = cbCardDataSort[n];

			int iGetTenCount = 0;

			for (int iten = 0; iten < cbCardCount; iten++)
			{
				if (GetCardLogicValue(cbCardDataSort[iten]) == 10 || GetCardLogicValue(cbCardDataSort[iten]) == 11)
				{
					iGetTenCount++;

				}
			}

			if (iGetTenCount >= 3)
			{
				if (GetCardColor(cbCardDataSort[0]) == 0x04 && GetCardColor(cbCardDataSort[1]) == 0x04)
				{
					if (bcOutCadData != null)
					{
						bcOutCadData[0] = cbCardDataSort[0];
						bcOutCadData[1] = cbCardDataSort[3];
						bcOutCadData[2] = cbCardDataSort[4];
						bcOutCadData[3] = cbCardDataSort[1];
						bcOutCadData[4] = cbCardDataSort[2];

					}
					return CT_SPECIAL_NIUNIUDW;

				}
				if (GetCardColor(cbCardDataSort[0]) == 0x04)
				{
					//大小王与最小的组合成牛
					if (bcOutCadData != null)
					{
						bcOutCadData[0] = cbCardDataSort[0];
						bcOutCadData[1] = cbCardDataSort[3];
						bcOutCadData[2] = cbCardDataSort[4];
						bcOutCadData[3] = cbCardDataSort[1];
						bcOutCadData[4] = cbCardDataSort[2];
					}
					if (cbCardDataSort[0] == 0x42)
						return CT_SPECIAL_NIUNIUDW;
					else
						return CT_SPECIAL_NIUNIUXW;
				}
				else
				{
					return RetType(GetCardLogicValue(cbCardDataSort[3]) + GetCardLogicValue(cbCardDataSort[4]));
				}

			}

			if (iGetTenCount == 2 || (iGetTenCount == 1 && GetCardColor(cbCardDataSort[0]) == 0x04))
			{

				if (GetCardColor(cbCardDataSort[0]) == 0x04 && GetCardColor(cbCardDataSort[1]) == 0x04)
				{
					if (bcOutCadData != null)
					{
						bcOutCadData[0] = cbCardDataSort[0];
						bcOutCadData[1] = cbCardDataSort[3];
						bcOutCadData[2] = cbCardDataSort[4];
						bcOutCadData[3] = cbCardDataSort[1];
						bcOutCadData[4] = cbCardDataSort[2];
					}
					return CT_SPECIAL_NIUNIUDW;
				}
				else
				{
					//如果有一张王 其他任意三张组合为10则是牛牛
					if (GetCardColor(cbCardDataSort[0]) == 0x04)
					{

						for (n = 1; n < cbCardCount; n++)
						{
							for (int j = 1; j < cbCardCount; j++)
							{
								if (j != n)
								{
									for (int w = 1; w < cbCardCount; w++)
									{
										if (w != n&&w != j)
										{
											//如果剩余的四张中任意三张能组合位10的整数倍

											if ((GetCardLogicValue(cbCardDataSort[n]) + GetCardLogicValue(cbCardDataSort[j]) + GetCardLogicValue(cbCardDataSort[w])) % 10 == 0)
											{

												int add = 0;
												for (int y = 1; y < cbCardCount; y++)
												{
													if (y != n&&y != j&&y != w)
													{
														iSingleA[add] = cbCardDataSort[y];
														add++;

													}

												}
												if (bcOutCadData != null)
												{
													bcOutCadData[0] = cbCardDataSort[n];
													bcOutCadData[1] = cbCardDataSort[j];
													bcOutCadData[2] = cbCardDataSort[w];
													bcOutCadData[3] = cbCardDataSort[0];
													bcOutCadData[4] = iSingleA[0];
												}
												if (cbCardDataSort[0] == 0x42)
													return CT_SPECIAL_NIUNIUDW;
												else
													return CT_SPECIAL_NIUNIUXW;


											}
										}
									}
								}
							}
						}

						//如果有一张王 其他任意三张组合不为10则 取两张点数最大的组合
						int bcTmp[] = new int[4];
						int iBig = 0;
						int in = 0;
						for (in = 1; in < cbCardCount; in++)
						{
							for (int j = 1; j < cbCardCount; j++)
							{
								if (in != j)
								{
									int bclogic =(int) ((GetCardLogicValue(cbCardDataSort[in]) + GetCardLogicValue(cbCardDataSort[j])) % 10);
									if (bclogic > iBig)
									{
										iBig = bclogic;
										int add = 0;
										bcTmp[0] = cbCardDataSort[in];
										bcTmp[1] = cbCardDataSort[j];
										for (int y = 1; y < cbCardCount; y++)
										{
											if (y != in&&y != j)
											{
												iSingleA[add] = cbCardDataSort[y];
												add++;
											}

										}
										bcTmp[2] = iSingleA[0];
										bcTmp[3] = iSingleA[1];

									}

								}
							}
						}

						if (bcOutCadData != null)
						{
							bcOutCadData[0] = cbCardDataSort[0];
							bcOutCadData[1] = bcTmp[2];
							bcOutCadData[2] = bcTmp[3];
							bcOutCadData[3] = bcTmp[0];
							bcOutCadData[4] = bcTmp[1];
						}
						if (iGetTenCount == 1 && GetCardColor(cbCardDataSort[0]) == 0x04)
						{
							//下面还能组合 有两张为 10 也可以组合成牛牛

						}
						else
						{
							//如果没有则比较 完与最小组合最大点数和组合
							return RetType(GetCardLogicValue(bcTmp[0]) + GetCardLogicValue(bcTmp[1]));
						}


			}
			else
			{
				if ((GetCardLogicValue(cbCardDataSort[2]) + GetCardLogicValue(cbCardDataSort[3]) + GetCardLogicValue(cbCardDataSort[4])) % 10 == 0)
				{
					if (bcOutCadData != null)
					{
						bcOutCadData[0] = cbCardDataSort[2];
						bcOutCadData[1] = cbCardDataSort[3];
						bcOutCadData[2] = cbCardDataSort[4];
						bcOutCadData[3] = cbCardDataSort[0];
						bcOutCadData[4] = cbCardDataSort[1];
					}
					return CT_SPECIAL_NIUNIU;
				}
				else
				{
					for (n = 2; n < cbCardCount; n++)
					{
						for (int j = 2; j < cbCardCount; j++)
						{
							if (j != n)
							{
								if ((GetCardLogicValue(cbCardDataSort[n]) + GetCardLogicValue(cbCardDataSort[j])) % 10 == 0)
								{
									int add = 0;
									for (int y = 2; y < cbCardCount; y++)
									{
										if (y != n&&y != j)
										{
											iSingleA[add] = cbCardDataSort[y];
											add++;

										}
									}
									if (iBigValue <= iSingleA[0] % 10)
									{
										iBigValue = GetCardLogicValue(iSingleA[0]) % 10;
										if (bcOutCadData != null)
										{
											bcOutCadData[0] = cbCardDataSort[0];
											bcOutCadData[1] = cbCardDataSort[n];
											bcOutCadData[2] = cbCardDataSort[j];
											bcOutCadData[3] = cbCardDataSort[1];
											bcOutCadData[4] = iSingleA[0];

										}

										if (iBigValue == 0)
										{

											return CT_SPECIAL_NIUNIU;
										}
									}

								}
							}
						}
					}
					if (iBigValue != 0)
					{
						return RetType(iBigValue);
					}
				}
			}

			}

			iGetTenCount = 1;

			}

			//4个组合
			if (iGetTenCount == 1)
			{

				if (GetCardColor(cbCardDataSort[0]) == 0x04)
				{

					for (n = 1; n < cbCardCount; n++)
					{
					for (int j = 1; j < cbCardCount; j++)
					{
					if (j != n)
					{
					//任意两张组合成牛
					if ((GetCardLogicValue(cbCardDataSort[n]) + GetCardLogicValue(cbCardDataSort[j])) % 10 == 0)
					{
					int add = 0;
					for (int y = 1; y < cbCardCount; y++)
					{
					if (y != n&&y != j)
					{
					iSingleA[add] = cbCardDataSort[y];
					add++;

					}

					}

					if (bcOutCadData != null)
					{
					bcOutCadData[0] = cbCardDataSort[0];
					bcOutCadData[1] = iSingleA[0];
					bcOutCadData[2] = iSingleA[1];
					bcOutCadData[3] = cbCardDataSort[n];
					bcOutCadData[4] = cbCardDataSort[j];
					}
					if (cbCardDataSort[0] == 0x42)
					return CT_SPECIAL_NIUNIUDW;
					else
					return CT_SPECIAL_NIUNIUXW;

					}
					}

					}
					}


					//取4张中组合最大的点数

					int bcTmp[] = new int[4];
					int iBig = 0;
					int in = 0;
					for (in = 1; in < cbCardCount; in++)
					{
						for (int j = 1; j < cbCardCount; j++)
						{
							if (in != j)
							{
								int bclogic =(int)( (GetCardLogicValue(cbCardDataSort[in]) + GetCardLogicValue(cbCardDataSort[j])) % 10);
								if (bclogic > iBig)
								{
									iBig = bclogic;
									int add = 0;
									bcTmp[0] = cbCardDataSort[in];
									bcTmp[1] = cbCardDataSort[j];
									for (int y = 1; y < cbCardCount; y++)
									{
										if (y != in&&y != j)
										{
											iSingleA[add] = cbCardDataSort[y];
											add++;
										}

									}
									bcTmp[2] = iSingleA[0];
									bcTmp[3] = iSingleA[1];

								}

							}
						}
					}

					if (bcOutCadData != null)
					{
						bcOutCadData[0] = cbCardDataSort[0];
						bcOutCadData[1] = bcTmp[2];
						bcOutCadData[2] = bcTmp[3];
						bcOutCadData[3] = bcTmp[0];
						bcOutCadData[4] = bcTmp[1];
					}
					return RetType(GetCardLogicValue(bcTmp[0]) + GetCardLogicValue(bcTmp[1]));

				}
				//取4张中任两张组合为10 然后求另外两张的组合看是否是组合中最大
				for (n = 1; n < cbCardCount; n++)
				{
					for (int j = 1; j < cbCardCount; j++)
					{
						if (j != n)
						{
							if ((GetCardLogicValue(cbCardDataSort[n]) + GetCardLogicValue(cbCardDataSort[j])) % 10 == 0)
							{
								int add = 0;
								for (int y = 1; y < cbCardCount; y++)
								{
									if (y != n&&y != j)
									{
										iSingleA[add] = cbCardDataSort[y];
										add++;

									}
								}
								if (iBigValue <= (GetCardLogicValue(iSingleA[0]) + GetCardLogicValue(iSingleA[1])) % 10)
								{
									iBigValue = GetCardLogicValue(iSingleA[0]) + GetCardLogicValue(iSingleA[1]) % 10;
									bcMakeMax[0] = cbCardDataSort[0];
									bcMakeMax[1] = cbCardDataSort[j];
									bcMakeMax[2] = cbCardDataSort[n];
									bcMakeMax[3] = iSingleA[0];
									bcMakeMax[4] = iSingleA[1];

									if (bcOutCadData != null)
									{
										CopyMemory(bcOutCadData, bcMakeMax, cbCardCount);
									}
									if (iBigValue == 0)
									{

										return CT_SPECIAL_NIUNIU;
									}
								}

							}
						}
					}
				}
				if (iBigValue != 0)
				{
					return RetType(iBigValue);
				}
				else
				{
					//如果组合不成功
					iGetTenCount = 0;
				}

			}
			if (iGetTenCount == 0)
			{
				//5个组合
				for (n = 0; n < cbCardCount; n++)
				{
					for (int j = 0; j < cbCardCount; j++)
					{
						if (j != n)
						{
							for (int w = 0; w < cbCardCount; w++)
							{
								if (w != n&&w != j)
								{
									int valueAdd = GetCardLogicValue(cbCardDataSort[n]);
									valueAdd += GetCardLogicValue(cbCardDataSort[j]);
									valueAdd += GetCardLogicValue(cbCardDataSort[w]);

									if (valueAdd % 10 == 0)
									{
										int add = 0;
										for (int y = 0; y < cbCardCount; y++)
										{
											if (y != n&&y != j&&y != w)
											{
												iSingleA[add] = cbCardDataSort[y];
												add++;

											}

										}
										if (iBigValue <= (GetCardLogicValue(iSingleA[0]) + GetCardLogicValue(iSingleA[1])) % 10)
										{
											iBigValue = GetCardLogicValue(iSingleA[0]) + GetCardLogicValue(iSingleA[1]) % 10;
											bcMakeMax[0] = cbCardDataSort[n];
											bcMakeMax[1] = cbCardDataSort[j];
											bcMakeMax[2] = cbCardDataSort[w];
											bcMakeMax[3] = iSingleA[0];
											bcMakeMax[4] = iSingleA[1];

											if (bcOutCadData != null)
											{
												CopyMemory(bcOutCadData, bcMakeMax, cbCardCount);
											}
											if (iBigValue == 0)
											{

												return CT_SPECIAL_NIUNIU;
											}
										}

									}

								}
							}
						}
					}
				}
				if (iBigValue != 0)
				{
					return RetType(iBigValue);
				}
				else
				{
					return CT_POINT;
				}

			}

			return CT_POINT;
		}
		
		
		// 分析扑克
		public void AnalysebCardData(int cbCardData[], int cbCardCount, tagAnalyseResult AnalyseResult) {
			// 设置结果
			AnalyseResult.clear();

			// 扑克分析
			for (int i = 0; i < cbCardCount; i++) {
				// 变量定义
				
				int cbSameCardData[] = { cbCardData[i], 0, 0, 0, 0 };
				int cbLogicValue = GetCardValue(cbCardData[i]);

				if(cbLogicValue != 0)
				{
					int cbSameCount = 1;
					// 获取同牌
					for (int j = i + 1; j < cbCardCount; j++) {
						// 逻辑对比
						if (GetCardValue(cbCardData[j]) != cbLogicValue) {
							break;
						}
						if(cbCardData[j] != 0)
						{
							// 设置扑克
							cbSameCardData[cbSameCount++] = cbCardData[j];					
						}
					}
				


				// 保存结果
				switch (cbSameCount) {
				case 1: // 单张
				{
					AnalyseResult.cbSignedLogicVolue[AnalyseResult.cbSignedCount] = cbLogicValue;
					for (int k = 0; k < cbSameCount; k++) {
						AnalyseResult.cbSignedCardData[(AnalyseResult.cbSignedCount) * cbSameCount + k] = cbSameCardData[k];
					}
					AnalyseResult.cbSignedCount++;
					break;
				}
				case 2: // 两张
				{
					AnalyseResult.cbLONGLogicVolue[AnalyseResult.cbLONGCount] = cbLogicValue;
					for (int k = 0; k < cbSameCount; k++) {
						AnalyseResult.cbLONGCardData[(AnalyseResult.cbLONGCount) * cbSameCount + k] = cbSameCardData[k];
					}
					AnalyseResult.cbLONGCount++;
					break;
				}
				case 3: // 三张
				{
					AnalyseResult.cbThreeLogicVolue[AnalyseResult.cbThreeCount] = cbLogicValue;
					for (int k = 0; k < cbSameCount; k++) {
						AnalyseResult.cbThreeCardData[(AnalyseResult.cbThreeCount) * cbSameCount + k] = cbSameCardData[k];
					}
					AnalyseResult.cbThreeCount++;
					break;
				}
				case 4: // 四张
				{
					AnalyseResult.cbFourLogicVolue[AnalyseResult.cbFourCount] = cbLogicValue;
					for (int k = 0; k < cbSameCount; k++) {
						AnalyseResult.cbFourCardData[(AnalyseResult.cbFourCount) * cbSameCount + k] = cbSameCardData[k];
					}
					AnalyseResult.cbFourCount++;
					break;
				}
				}

				// 设置递增
				i += cbSameCount - 1;
				}

			}

			return;
		}

		//大小比较
		/*
		cbNextCardData>cbFirstCardData  返回1
		cbNextCardData<cbFirstCardData  返回-1
		cbNextCardData==cbFirstCardData 返回0
		*/
		//Multiple 比较出来的倍数
		private void memset(int[] bcMakeMax, int d, int length) {
			for(int i = 0;i<length;i++){
				bcMakeMax[i] = (int)d;
			}
		}
		public String GetCardStringType(int cbFirstCardData[], int cbFirstCardCount){
			int cbFirstCardType = GetCardType(cbFirstCardData, cbFirstCardCount);
			switch(cbFirstCardType){
				case CT_SPECIAL_NIU7:
					return "牛七";
				case CT_SPECIAL_NIU8:
					return "牛八";
				case CT_SPECIAL_NIU9:
					return "牛九";
				case CT_SPECIAL_NIUNIU:
					return "牛牛";
				case CT_SPECIAL_NIUNIUXW:
					return "";
				case CT_SPECIAL_NIUNIUDW:
					return "";
				case CT_SPECIAL_NIUYING:
					return "";
				case CT_SPECIAL_SHUNZI:
					return "顺子牛";
				case CT_SPECIAL_TONGHUA:
					return "同花牛";
				case CT_SPECIAL_HULU:
					return "葫芦牛";
				case CT_SPECIAL_WUXIAONIU:
					return "无小牛";
				case CT_SPECIAL_WUHUANIU:
					return "五花牛";
				case CT_SPECIAL_BOMEBOME:
					return "炸弹牛";
				case CT_SPECIAL_TONGHUASHU:
					return "同花顺";
				case CT_SPECIAL_CT_KING_TONG_HUA_SHUN:
					return "皇室同花顺";
			}
			return "";
		}
		int CompareCard( int cbFirstCardData[], int cbFirstCardCount,  int cbNextCardData[], int cbNextCardCount, int Multiple)
		{
			//合法判断
			if (!(5 == cbFirstCardCount && 5 == cbNextCardCount)) return 0;

			//获取牌型
			int cbFirstCardType = GetCardType(cbFirstCardData, cbFirstCardCount);
			int cbNextCardType = GetCardType(cbNextCardData, cbNextCardCount);

			//牌型比较
			if (cbFirstCardType != cbNextCardType)
			{
				if (cbNextCardType > cbFirstCardType)
				{
					if (cbNextCardType >= 12)
					{
						Multiple = 10;

					}
					else
					{
						Multiple = (int)(cbNextCardType - 2);

					}
					return 1;

				}
				else
				{
					if (cbFirstCardType >= 12)
					{
						Multiple = 10;

					}
					else
					{
						Multiple =(int) (cbFirstCardType - 2);

					}
					return -1;
				}
			}

			//特殊牌型判断
			if (CT_POINT != cbFirstCardType && cbFirstCardType == cbNextCardType)
			{
				//排序扑克
				int cbFirstCardDataTmp[] = new int[NIUNIU_CARD_COUNT];
				int cbNextCardDataTmp[] = new int[NIUNIU_CARD_COUNT];

				CopyMemory(cbFirstCardDataTmp, cbFirstCardData, cbFirstCardCount);
				CopyMemory(cbNextCardDataTmp, cbNextCardData, cbNextCardCount);
				SortCardList(cbFirstCardDataTmp, cbFirstCardCount, ST_NEW);
				SortCardList(cbNextCardDataTmp, cbNextCardCount, ST_NEW);

				if (cbFirstCardType >= 12)
				{
					Multiple = 10;

				}
				else
				{
					Multiple = (int)(cbFirstCardType - 2);

				}
				
				int firstValue = GetCardNewValue(cbFirstCardDataTmp[0]);
				int NextValue = GetCardNewValue(cbNextCardDataTmp[0]);

				if(cbFirstCardType == cbNextCardType && cbFirstCardType == CT_SPECIAL_HULU )
				{
					 firstValue = GetCardNewValue(cbFirstCardDataTmp[2]);
					 NextValue = GetCardNewValue(cbNextCardDataTmp[2]);
				}
				
				if(cbFirstCardType == cbNextCardType && cbFirstCardType == CT_SPECIAL_BOMEBOME )
				{
					 firstValue = GetCardNewValue(cbFirstCardDataTmp[1]);
					 NextValue = GetCardNewValue(cbNextCardDataTmp[1]);
				}
				
				int firstColor = GetCardColor(cbFirstCardDataTmp[0]);

				int NextColor = GetCardColor(cbNextCardDataTmp[0]);


				if (firstValue < NextValue)
				{
					return 1;
				}
				else
				{
					if (firstValue == NextValue)
					{
						if (firstColor < NextColor)
						{
							return 1;

						}
						else
						{
							return -1;
						}
					}
					return -1;
				}

			}

			//排序扑克
			int cbFirstCardDataTmp[] = new int[NIUNIU_CARD_COUNT];
			int cbNextCardDataTmp[] = new int[NIUNIU_CARD_COUNT];
			CopyMemory(cbFirstCardDataTmp, cbFirstCardData, cbFirstCardCount);
			CopyMemory(cbNextCardDataTmp, cbNextCardData, cbNextCardCount);
			SortCardList(cbFirstCardDataTmp, cbFirstCardCount, ST_NEW);
			SortCardList(cbNextCardDataTmp, cbNextCardCount, ST_NEW);

			int firstValue = GetCardNewValue(cbFirstCardDataTmp[0]);
			int NextValue = GetCardNewValue(cbNextCardDataTmp[0]);
			int firstColor = GetCardColor(cbFirstCardDataTmp[0]);
			int NextColor = GetCardColor(cbNextCardDataTmp[0]);

			if (firstValue < NextValue)
			{
				return 1;
			}
			else
			{
				if (firstValue == NextValue)
				{
					if (firstColor < NextColor)
					{
						return 1;

					}
					else
					{
						return -1;
					}
				}
				return -1;
			}

		}

		//获取牌点
		int GetCardListPip( int cbCardData[], int cbCardCount)
		{
			//变量定义
			int cbPipCount = 0;

			//获取牌点
			int cbCardValue = 0;
			for (int i = 0; i < cbCardCount; i++)
			{
				cbCardValue = GetCardValue(cbCardData[i]);
				if (cbCardValue > 10)
				{
					cbCardValue = 10;

				}
				cbPipCount += cbCardValue;
			}
			return (int)(cbPipCount % 10);
		}
		int 	LOGIC_MASK_COLOR		=	0xF0		;						//花色掩码
		int	LOGIC_MASK_VALUE	=		0x0F	;							//数值掩码
		//获取数值;
		int GetCardValue(int cbCardData)
		{
			
			return (int)(cbCardData&LOGIC_MASK_VALUE);
		}
		//获取花色
		int GetCardColor(int cbCardData)
		{
			return (int)((cbCardData&LOGIC_MASK_COLOR) >> 4);
		}		
		int GetCardNewValue(int cbCardData)
		{
			//扑克属性
			int cbCardColor = GetCardColor(cbCardData);
			int cbCardValue = GetCardValue(cbCardData);

			//转换数值
			if (cbCardColor == 0x04) return (int)(cbCardValue + 13 + 2);
			return cbCardValue;

		}
		
		
		//逻辑大小
		int GetCardLogicValue(int cbCardData)
		{
			int cbValue = GetCardValue(cbCardData);

			//获取花色
			int cbColor = GetCardColor(cbCardData);

			if (cbValue > 10)
			{
				cbValue = 10;

			}
			if (cbColor == 0x4)
			{
				return 11;
			}
			return cbValue;
		}

		//排列扑克
		void SortCardList(int cbCardData[], int cbCardCount, int cbSortType)
		{
			//数目过虑
			if (cbCardCount == 0) return;

			//转换数值
			int cbSortValue[] = new int[NIUNIU_CARD_COUNT];
			if (ST_VALUE == cbSortType)
			{
				for (int i = 0; i < cbCardCount; i++) cbSortValue[i] = GetCardValue(cbCardData[i]);
			}
			else
			{
				if (cbSortType == ST_NEW)
				{
					for (int i = 0; i < cbCardCount; i++) cbSortValue[i] = GetCardNewValue(cbCardData[i]);

				}
				else
				{
					for (int i = 0; i < cbCardCount; i++) cbSortValue[i] = GetCardLogicValue(cbCardData[i]);

				}

			}


			//排序操作
			boolean bSorted = true;
			int cbThreeCount;
			int  cbLast = (int)(cbCardCount - 1);
			do
			{
				bSorted = true;
				for (int i = 0; i < cbLast; i++)
				{
					if ((cbSortValue[i] < cbSortValue[i + 1]) ||
						((cbSortValue[i] == cbSortValue[i + 1]) && (cbCardData[i] < cbCardData[i + 1])))
					{
						//交换位置
						cbThreeCount = cbCardData[i];
						cbCardData[i] = cbCardData[i + 1];
						cbCardData[i + 1] = cbThreeCount;
						cbThreeCount = cbSortValue[i];
						cbSortValue[i] = cbSortValue[i + 1];
						cbSortValue[i + 1] = cbThreeCount;
						bSorted = false;
					}
				}
				cbLast--;
			} while (bSorted == false);

			return;
		}

		//////////////////////////////////////////////////////////////////////////

}


